/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.oracleandsxnd.erp.xsd.service;

import com.oracleandsxnd.erp.common.persistence.Page;
import com.oracleandsxnd.erp.common.service.CrudService;
import com.oracleandsxnd.erp.common.utils.StringUtils;
import com.oracleandsxnd.erp.xsd.dao.XsglSaleDao;
import com.oracleandsxnd.erp.xsd.dao.XsglSaleDetailDao;
import com.oracleandsxnd.erp.xsd.entity.XsglSale;
import com.oracleandsxnd.erp.xsd.entity.XsglSaleDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 销售单Service
 * @author 销售单
 * @version 2019-12-16
 */
@Service
@Transactional(readOnly = true)
public class XsglSaleService extends CrudService<XsglSaleDao, XsglSale> {

	@Autowired
	private XsglSaleDetailDao xsglSaleDetailDao;
	
	public XsglSale get(String id) {
		XsglSale xsglSale = super.get(id);
		xsglSale.setXsglSaleDetailList(xsglSaleDetailDao.findList(new XsglSaleDetail(xsglSale)));
		return xsglSale;
	}
	
	public List<XsglSale> findList(XsglSale xsglSale) {
		return super.findList(xsglSale);
	}
	
	public Page<XsglSale> findPage(Page<XsglSale> page, XsglSale xsglSale) {
		return super.findPage(page, xsglSale);
	}
	
	@Transactional(readOnly = false)
	public void save(XsglSale xsglSale) {
		super.save(xsglSale);
		for (XsglSaleDetail xsglSaleDetail : xsglSale.getXsglSaleDetailList()){
			if (xsglSaleDetail.getId() == null){
				continue;
			}
			xsglSaleDetail.setCompany(xsglSale.getCompany());
			if (XsglSaleDetail.DEL_FLAG_NORMAL.equals(xsglSaleDetail.getDelFlag())){
				if (StringUtils.isBlank(xsglSaleDetail.getId())){
					xsglSaleDetail.setSale(xsglSale);
					xsglSaleDetail.preInsert();
					xsglSaleDetailDao.insert(xsglSaleDetail);
				}else{
					xsglSaleDetail.preUpdate();
					xsglSaleDetailDao.update(xsglSaleDetail);
				}
			}else{
				xsglSaleDetailDao.delete(xsglSaleDetail);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(XsglSale xsglSale) {
		super.delete(xsglSale);
		xsglSaleDetailDao.delete(new XsglSaleDetail(xsglSale));
	}

	public Page<XsglSaleDetail> findDPage(Page<XsglSaleDetail> page, XsglSaleDetail entity) {
		entity.setPage(page);
		page.setList(xsglSaleDetailDao.findList(entity));
		return page;
	}

	public Page<XsglSaleDetail> findtjPage(Page<XsglSaleDetail> page, XsglSaleDetail entity) {
		List<XsglSaleDetail> xsds=xsglSaleDetailDao.findtjList(entity);
		if(xsds!=null&&xsds.size()>0) {
			XsglSaleDetail xsd = new XsglSaleDetail();
			for (XsglSaleDetail d:xsds
				 ) {
				xsd.setName("小计");
				xsd.setAmount(xsd.getAmount()+d.getAmount());
				xsd.setTotalprice(xsd.getTotalprice()+d.getTotalprice());
				xsd.setCost(xsd.getCost()+d.getCost());
				xsd.setProfit(xsd.getProfit()+d.getProfit());
			}


			xsds.add(xsd);
		}
		entity.setPage(page);
		page.setList(xsds);
		return page;
	}
	@Transactional(readOnly = false)
    public void fp(XsglSale xsglSale) {
		dao.fp(xsglSale);
    }
}